package bookForJava.P5;

import java.util.Scanner;


public class P5_2 {
	static final int N=15;
	static void quickSort(int[] arr,int low,int high)			//快速排序算法
	{
		int start = low;
		int end = high;
		int key = arr[low];

		while(start<end){
			while(start<end && arr[end]>=key){
				end--;
			}
			if(arr[end]<key){
				int tmp = arr[end];
				arr[end] = key;
				arr[start] = tmp;
			}

			while(start<end && arr[start]<=key){
				start++;
			}
			if(arr[start]>key){
				int tmp = arr[start];
				arr[start] = key;
				arr[end] = tmp;
			}
		}

		if(low<start) quickSort(arr,low,start-1);
		if(end<high) quickSort(arr,end+1,high);
	}
	
	static int searchFun(int a[],int n,int x)					//折半查找
	{
	    int mid,low,high;

		low=0;
		high=n-1;
	    while(low<=high)
	    {
	   		mid=(low+high)/2;
			if(a[mid]==x)
	            return mid;						//找到
			else if(a[mid]>x)
			    high=mid-1;
	        else
				low=mid+1;
	    }

		return -1;								//未找到
	}
	
	public static void main1(String[] args) {
		int[] shuzu=new int[N];
		int x,n,i;

		for(i=0;i<N;i++)
		{
			shuzu[i]=(int)(100+Math.random()*(100+1));;				//产生数组
		}


		System.out.print("折半查找算法演示！\n");
	    System.out.print("排序前数据序列:\n");
	    for(i=0;i<N;i++)
		{
	        System.out.print(" "+shuzu[i]);					//输出序列
		}
		System.out.print("\n\n");
		quickSort(shuzu,0,N-1);				//排序
	    System.out.print("排序后数据序列:\n");
	    for(i=0;i<N;i++)
		{
	        System.out.print(" "+shuzu[i]);					//输出序列
		}
		System.out.print("\n\n");
	    System.out.print("输入要查找的数:");
	    Scanner input=new Scanner(System.in);
	    x=input.nextInt();							//输入要查找的数

	    n=searchFun(shuzu,N,x);						//查找

	    if(n<0)								//输出查找结果
		{
			System.out.println("没找到数据:"+x);
		}
		else
		{
			System.out.println("数据:"+x+" 位于数组的第"+(n+1)+" 个元素处。");
		}

	}

	public static void main(String[] args) {
//		Scanner input = new Scanner(System.in);
//		//获取输入的整数序列
//		String str = input.nextLine();
//		String[] strings = str.split(" ");
//		//转为整数数组
//		int[] arr = new int[strings.length];
//		for (int i = 0; i < strings.length; i++) {
//			arr[i] = Integer.parseInt(strings[i]);
//			System.out.println(arr[i]);
//		}

		StringBuffer sb = new StringBuffer("jack");
		sb.append(true);
		sb.append('a');
		sb.append(97).append(34.0).append(new char[]{'o','o'});   // 链式编程
		System.out.println( sb.toString() );          // 输出缓冲区的中文本数据
		sb = new StringBuffer("jack");
		sb.insert( 2, "java" );                       // jajavack
		System.out.println( sb.toString() );


	}


}
